package com.zhongya.example.view

import android.content.Context
import android.graphics.*
import android.graphics.Color.RED
import android.util.AttributeSet
import android.view.View

class FontView : View {

    val text = "harvic's blog"
    val mTop = 200f
    val baseLineX = 0f

    lateinit var paint : Paint

    constructor(context: Context) : super(context) {

    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        //设置paint
        paint = Paint()
        paint.textSize = 120f
        paint.textAlign = Paint.Align.LEFT
//        paint.strokeCap = Paint.Cap.BUTT

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        //画top线
        paint.color = Color.YELLOW
        canvas?.drawLine(baseLineX,mTop,3000f,mTop,paint)

        //计算出baseline线的位置
        val fm = paint.fontMetrics
        val baseLineY = mTop - fm.top

        //画基线
        paint.color = Color.RED
        canvas?.drawLine(baseLineX,baseLineY,3000f,baseLineY,paint)

        //写文字
        paint.color = Color.GREEN
        canvas?.drawText(text,baseLineX,baseLineY,paint)


    }



}